{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "189436a2",
   "metadata": {},
   "source": [
    "## 易错点\n",
    "此题中间隔了几天没想起来咋写了\n",
    "**中括号中的区间值为：**\n",
    "**range区间值为，**\n",
    "\n",
    "ma上跑不动"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "36836780",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pip install tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a5c6449f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入相关库\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import time\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "from subprocess import check_output\n",
    "import lightgbm as lgb\n",
    "import matplotlib.pyplot as plt\n",
    "from tqdm import tqdm_notebook as tqdm\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import confusion_matrix, log_loss\n",
    "from xgboost import XGBClassifier\n",
    "from lightgbm import LGBMClassifier\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.model_selection import cross_validate\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d948484",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#读取数据、\n",
    "df_forest = pd.read_csv('./data/train_forest_covertype.csv')\n",
    "#随机采样15120条数据\n",
    "df_forest = df_forest.sample(n=15120)\n",
    "n=15120\n",
    "df_forest_sample = df_forest.sample(n)\n",
    "df_forest_sample.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0c53cb41",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 查看抽样数据\n",
    "df_forest_sample.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9905a0f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#删除列 Soil_Type7 和 Soil_Tpel5\n",
    "df_forest_sample.drop(['Soil_Type7','Soil_Type15'],inplace=True,axis=1)\n",
    "df_forest_sample.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6a88f1a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "XGB = XGBClassifier()\n",
    "lgbm = LGBMClassifier()\n",
    "#存储模型\n",
    "first_models = [XGB,lgbm]\n",
    "# 模型名字\n",
    "first_model_names = ['XGB','lgbm']\n",
    "seed=42 \n",
    "skf =5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d800758e",
   "metadata": {},
   "source": [
    "1.ShuffleSplit 函数对数据进行切分，指定参数splitting_iterations 为 skf,test_size为0.3,train_size为0.6,random_state为seed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b40e9a82",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import ShuffleSplit\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "# Defining other steps\n",
    "n_folds = 5\n",
    "# 此行由考生填写\n",
    "skf = ShuffleSplit(n_splits=n_folds,test_size=0.3,train_size=0.6,random_state=seed)\n",
    "# 此行由考生填写\n",
    "std_sca = StandardScaler()\n",
    "X= df_forest_sample.drop(['Cover_Type'],axis=1)\n",
    "y= pd.factorize(df_forest_sample['Cover_Type'])[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f321af36",
   "metadata": {},
   "outputs": [],
   "source": [
    "MLA_columns = ['MLA Name', 'MLA Parameters','MLA Train Accuracy Mean', 'MLA Test Accuracy Mean', 'MLA Time']\n",
    "MLA_compare = pd.DataFrame(columns = MLA_columns)\n",
    "#create table to compare MLA predictions\n",
    "MLA_predict = df_forest_sample[['Id']]\n",
    "train_size = X.shape[0]\n",
    "n_models = len(first_models)\n",
    "oof_pred =np.zeros((train_size,n_models))\n",
    "scores = []\n",
    "row_index=0\n",
    "MLA_compare"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea497fe0",
   "metadata": {},
   "source": [
    "2.使用Pipeline进行模型训练set中指定标准为为('Scaler',std_sca)，模型为('Estimator',model) \n",
    "3.使用cross_validate函数对模型进行评分，模型使用model，数据集使用X，y,指定return_train_score为True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f535759f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[22:04:21] WARNING: ../src/learner.cc:1095: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'multi:softprob' was changed from 'merror' to 'mlogloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "[22:36:50] WARNING: ../src/learner.cc:1095: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'multi:softprob' was changed from 'merror' to 'mlogloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n"
     ]
    }
   ],
   "source": [
    "for n, model in enumerate(first_models):\n",
    "    # 由考生填写\n",
    "    model_pipeline = Pipeline(steps=[('Scaler',std_sca),('Estimator',model)])\n",
    "    # 由考生填写\n",
    "    MLA_name = model.__class__.__name__\n",
    "    MLA_compare.loc[row_index,'MLA Name'] = MLA_name\n",
    "    MLA_compare.loc[row_index,'MLA Parameters'] = str(model.get_params())\n",
    "    # 由考生填写\n",
    "    cv_results = cross_validate(estimator=model,X=X,y=y,return_train_score=True)\n",
    "    # 由考生填写\n",
    "    MLA_compare.loc[row_index,'MLA Time'] = cv_results[ 'fit_time' ].mean()\n",
    "    MLA_compare.loc[row_index,'MLA Train Accuracy Mean'] = cv_results['train_score'].mean()\n",
    "    MLA_compare.loc[row_index,'MLA Test Accuracy Mean'] = cv_results['test_score'].mean()\n",
    "    model_pipeline.fit(X,y)\n",
    "    MLA_predict[MLA_name] = model_pipeline.predict(X)\n",
    "    row_index+=1  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfb3f07a",
   "metadata": {},
   "source": [
    "4.使用 sort_values对MLA_compare 按照集 MLA Test Accuracy Mean 这-列倒序排列,指定 inplace 为 True 覆盖原本数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1021dcd2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 由考生填写\n",
    "MLA_compare.sort_values(by=['MLA Test Accuracy Mean'],ascending=False,inplace=True)\n",
    "# 由考生填写\n",
    "MLA_compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f68dcb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "MLA_compare.index[-20:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e2d1915",
   "metadata": {},
   "outputs": [],
   "source": [
    "MLA_compare.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce85d9ec",
   "metadata": {},
   "source": [
    "5.删除 MLA compare 后20 位数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09ebb7d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 由考生填写\n",
    "MLA_compare.drop(axis=0,index=MLA_compare.index[-20:-1])\n",
    "# 由考生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7f7a9c6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PySpark-2.4.5",
   "language": "python",
   "name": "pyspark-2.4.5"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
